package com.kanshu.ksgb.fastread.doudou.module.reader.page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by newbiechen on 17-7-1.
 */

public class TxtPage {
    // 创建对象池
    private static final List<TxtPage> sPool = new ArrayList<>();
    private static final int POOL_SIZE = 30;
    public int position;// 页数索引
    public String title; // 章节名称
    public int titleLines; // 当前标题所占的行数
    public List<String> lines = new ArrayList<>(); // 内容行数
    // 章节序号
    public int chapterIndex;
    // 书籍标题
    public String bookTitle;
    // 是否是扉页
    public boolean isHeadPage;


    /**
     * 从对象池中获取对象
     * @return
     */
    public static TxtPage obtain() {
        synchronized (sPool) {
            int size = sPool.size();
            if (size > 0) {
                return sPool.remove(sPool.size() - 1);
            }
        }
        return new TxtPage();
    }

    /**
     * 将使用完的对象放入对象池中
     * @param page
     */
    public static void release(TxtPage page) {
        synchronized (sPool) {
            for (TxtPage txtPage : sPool) {
                if (page == txtPage) {
                    return;
                }
            }
            if (sPool.size() < POOL_SIZE) {
                page.reset();
                sPool.add(page);
            }
        }
    }

    public void recycle() {
        release(this);
    }

    public void reset() {
        isHeadPage = false;
        lines.clear();
    }
    /**
     * 是否是扉頁
     * @param page
     * @return
     */
    public static boolean isHeadpage(TxtPage page) {
        return page != null && page.isHeadPage;
    }

}
